package edu.jmu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.jmu.domain.Admin;
import edu.jmu.domain.News;
import edu.jmu.service.NewsService;
import edu.jmu.service.impl.PictureServiceImpl;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    private NewsService ns;

    @Autowired
    private PictureServiceImpl ps;

    @RequestMapping("ajaxNewsInfo")
    public @ResponseBody
    Map<String, Object> ajaxNewsInfo(int index) {
        PageHelper.startPage(index, 10);
        List<News> newsList = ns.findAll();
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        map.put("size", pageInfo.getSize());
        map.put("total", pageInfo.getTotal());
        map.put("pages", pageInfo.getPages());
        List<News> list = new ArrayList<>(pageInfo.getList());
        map.put("list", list);
        map.put("isFirstPage", pageInfo.isIsFirstPage());
        map.put("isLastPage", pageInfo.isIsLastPage());
        map.put("hasPreviousPage", pageInfo.isHasPreviousPage());
        map.put("hasNextPage", pageInfo.isHasNextPage());
        return map;
    }

    @RequestMapping("findByKeyword")
    public @ResponseBody
    Map<String, Object> findByLike(@RequestParam("key") String keyword, int index) {
        PageHelper.startPage(index, 10);
        List<News> newsList = ns.findByKeyword("%" + keyword + "%");
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        map.put("size", pageInfo.getSize());
        map.put("total", pageInfo.getTotal());
        map.put("pages", pageInfo.getPages());
        List<News> list = new ArrayList<>(pageInfo.getList());
        map.put("list", list);
        map.put("isFirstPage", pageInfo.isIsFirstPage());
        map.put("isLastPage", pageInfo.isIsLastPage());
        map.put("hasPreviousPage", pageInfo.isHasPreviousPage());
        map.put("hasNextPage", pageInfo.isHasNextPage());
        return map;
    }

    @RequestMapping("findByKeywordDrafts")
    public @ResponseBody
    Map<String, Object> findByKeywordDrafts(@RequestParam("key") String keyword, int index) {
        PageHelper.startPage(index, 10);
        List<News> newsList = ns.findByKeywordDrafts("%" + keyword + "%");
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        map.put("size", pageInfo.getSize());
        map.put("total", pageInfo.getTotal());
        map.put("pages", pageInfo.getPages());
        List<News> list = new ArrayList<>(pageInfo.getList());
        map.put("list", list);
        map.put("isFirstPage", pageInfo.isIsFirstPage());
        map.put("isLastPage", pageInfo.isIsLastPage());
        map.put("hasPreviousPage", pageInfo.isHasPreviousPage());
        map.put("hasNextPage", pageInfo.isHasNextPage());
        return map;
    }

    @RequestMapping("addNews")
    public String addNews(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin!=null){
            int id = ns.addNews();
            model.addAttribute("id", id);
            return "add_news";
        }
        request.getRequestDispatcher("/admin_login.jsp").forward(request,response);
        return null;
    }

    @RequestMapping("upload")
    public @ResponseBody
    String upload(MultipartFile upload, HttpServletRequest request, int id) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/images/");
        File file = new File(path);
        if (!file.exists()) {
            //如果文件夹不存在则创建文件夹
            file.mkdirs();
        }
        String filename = upload.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + "_" + filename;
        upload.transferTo(new File(path, filename));
        ps.savePicture(path + filename, id);
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        String contextPath = request.getContextPath();
        array.put(contextPath + "/images/" + filename);
        json.put("errno", 0);
        json.put("data", array);
        return json.toString();
    }

    @RequestMapping("updateNews")
    public @ResponseBody
    Map<String, String> updateNews(News news) {
        news.setReleaseDate(new Date());
        Map<String, String> map = new HashMap<>();
        ns.updateNews(news);
        map.put("msg", "自动更新成功");
        return map;
    }

    @RequestMapping("releaseNews")
    public
    String releaseNews(News news){
        news.setReleaseDate(new Date());
        ns.releaseNews(news);
        return "news_info";
    }

    @RequestMapping("deleteNews")
    public @ResponseBody Map<String,String> deleteNews(@RequestParam("list[]") List<Integer> list){
        ns.deleteNews(list);
        ps.deletePicture(list);
        Map<String,String> map = new HashMap<>();
        map.put("msg","删除成功！");
        return map;
    }

    @RequestMapping("getNewsInfo")
    public @ResponseBody News getNewsInfo(@RequestParam("id")Integer id){
        return ns.findById(id);
    }

    @RequestMapping("modifyNews")
    public String modifyNews(Integer id,Model model){
        model.addAttribute("id", id);
        return "modify_news";
    }

    @RequestMapping("showDraftsInfo")
    public @ResponseBody Map<String, Object> showDraftsInfo(int index){
        PageHelper.startPage(index, 10);
        List<News> newsList = ns.findDraftsInfo();
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        map.put("size", pageInfo.getSize());
        map.put("total", pageInfo.getTotal());
        map.put("pages", pageInfo.getPages());
        List<News> list = new ArrayList<>(pageInfo.getList());
        map.put("list", list);
        map.put("isFirstPage", pageInfo.isIsFirstPage());
        map.put("isLastPage", pageInfo.isIsLastPage());
        map.put("hasPreviousPage", pageInfo.isHasPreviousPage());
        map.put("hasNextPage", pageInfo.isHasNextPage());
        return map;
    }

    @RequestMapping("news")
    public String news(Integer id,Model model){
        model.addAttribute("id",id);
        return "news";
    }








}
